<?php

App::uses('AppController', 'Controller');

class MessagesController extends AppController
{
    public $name = 'Messages';
    public $uses = array('Message', 'Scrap');

    function index()
    {
     
    }
    
    
    function add()
    {
        if($this->request->is('post'))
        {
            if(empty($this->data['Message']['description']))
            {
                $this->Session->setFlash('<div class="alert alert-error">Arquivo não selecionado</div>');
            }
            else
            {
                $this->request->data['Message']['user_id'] = 1;

                $this->Scrap->save(array(
                    'description' => $this->request->data['Message']['description'],
                    'type' => 'message',
                    'user_id'=>1
                ));

                $this->request->data['Message']['scrap_id'] = $this->Scrap->getLastInsertID();

                $this->Message->save($this->request->data);

                $this->Session->setFlash('<div class="alert alert-success">Enviado com sucesso!</div>');
//                $this->redirect(array('controller' => 'images', 'action' => 'view', $this->Message->getLastInsertID()));
                $this->redirect(array('controller' => 'pages', 'action' => 'index'));
            }
        }
        
    }
    
    function view($id)
    {
        $image = $this->Message->find('first', array('conditions'=>array('id'=>$id)));
        $this->set('image', $image);
        
    }
}
